package com.xiaoniu.forum.web.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xiaoniu.forum.common.utils.RemoveIdsUtils;
import com.xiaoniu.forum.entity.vo.CommunityTopicOptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xiaoniu.forum.entity.CommunityCategoryEntity;
import com.xiaoniu.forum.service.CommunityCategoryService;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.R;



/**
 * 社区分类表，比如：
Java、C语言这些社区都属于“编程”这一分类
 *
 * @author xiaoniu
 * @email sunlightcs@gmail.com
 * @date 2021-03-16 15:11:51
 */
@RestController
@RequestMapping("forum/communitycategory")
public class CommunityCategoryController {
    @Autowired
    private CommunityCategoryService communityCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("forum:communitycategory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = communityCategoryService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 所有
     */
    @RequestMapping("/all")
    public R all(){
        List<CommunityCategoryEntity> list = communityCategoryService.list();
        return R.ok().put("data",list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("forum:communitycategory:info")
    public R info(@PathVariable("id") String id){
		CommunityCategoryEntity communityCategory = communityCategoryService.getById(id);

        return R.ok().put("communityCategory", communityCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("forum:communitycategory:save")
    public R save(@RequestBody CommunityCategoryEntity communityCategory){
		communityCategoryService.save(communityCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("forum:communitycategory:update")
    public R update(@RequestBody CommunityCategoryEntity communityCategory){
		communityCategoryService.updateById(communityCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("forum:communitycategory:delete")
    public R delete(@RequestBody String[] ids){

        int preLen = ids.length;

        //删除之前会校验,是否与其他表关联 如有 则删除失败
        Integer failCount = communityCategoryService.removeByIds(Arrays.asList(ids));

        return RemoveIdsUtils.returnMsg(preLen,failCount);

    }

    /**
     * 根据id查询社区分类名称
     * @param id
     * @return 返回社区分类名称 String类型
     */
    @RequestMapping("communityCategory/{id}")
    public R getByCommunityCategoryById(@PathVariable String id){
        String communityCategoryName = communityCategoryService.getCommunityCategoryNameById(id);
        return R.ok().put("communityCategoryName",communityCategoryName);
    }

    /**
     * 根据分类名称查询
     * @param kw
     * @param params
     * @return
     */
    @RequestMapping("search/{kw}")
    public R searchByKw(@PathVariable String kw,@RequestParam Map<String, Object> params){
        PageUtils page = communityCategoryService.searchByKw(kw,params);
        return R.ok().put("page",page);
    }

    @GetMapping("/options")
    public R getAllWithOptions(){
        List<CommunityTopicOptionVo> list =  communityCategoryService.getAllWithOptions();

        return R.ok().put("data",list);
    }

}
